package com.dean.basic.sort;

import java.util.Arrays;

/**
 * 冒泡排序  相邻的两个元素比较大小尽心交换
 */
public class BubbleSort implements DeanArraySort{
    @Override
    public int[] sort(int[] srcArray) {
        int [] sortArr = Arrays.copyOf(srcArray,srcArray.length);
        int tmp;
        boolean change;
        for (int i = 1; i < sortArr.length; i++) {
            change = false;
            for (int j = 0; j < sortArr.length-i; j++) {
                if(sortArr[j] > sortArr[j+1]){
                    tmp = sortArr[j];
                    sortArr[j] = sortArr[j+1];
                    sortArr[j+1] = tmp;
                    change = true;
                }
            }
            if(!change){
                break;
            }
            System.out.println(Arrays.toString(sortArr));
        }
        return sortArr;
    }
}
